﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace SCM.Common.Extensions
{
    /// <summary>
    /// 通用帮助类
    /// </summary>
    public static class UnitityHelper
    {
        private static int billIndex = 0;
        /// <summary>
        /// 分页页码
        /// </summary>
        public static int PageSize = 10;

        public static T GetDescription<T>(Type type)
        {
            var attribute = type.GetCustomAttributes(typeof(T), false).FirstOrDefault();
            if (attribute == null)
            {
                return default(T);
            }
            return (T)attribute;
        }

        /// <summary>
        /// 打乱
        /// </summary>
        /// <param name="arrItem"></param>
        public static void ShuffleArray<T>(List<T> arrItem)
        {
            Random rnd = new Random(Guid.NewGuid().GetHashCode());
            int index = 0;
            T tmp = default(T);
            for (int i = 0; i < arrItem.Count; i++)
            {
                index = rnd.Next(arrItem.Count);
                if (index == i)
                {
                    if (index == 0)
                    {
                        index++;
                    }
                    else
                    {
                        index--;
                    }
                }
                tmp = arrItem[index];
                arrItem[index] = arrItem[i];
                arrItem[i] = tmp;
            }
        }

        public static string GetAreaProvinceCode(string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                throw new Exception("地区编码不能为空");
            }
            return code.Substring(0, 2);
        }
        /// <summary>
        /// 处理特殊地区code
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string AreaSortCodeToCityCode(string code)
        {
            if (code.StartsWith("11"))
            {
                return "11";
            }
            if (code.StartsWith("12"))
            {
                return "12";
            }
            if (code.StartsWith("50"))
            {
                return "50";
            }
            if (code.StartsWith("31"))
            {
                return "31";
            }
            if (code.StartsWith("71"))
            {
                return "71";
            }
            if (code.StartsWith("81"))
            {
                return "81";
            }
            if (code.StartsWith("82"))
            {
                return "82";
            }
            if (code.Length == 4)
            {
                return code;
            }
            if (code.Length > 4)
                return code.Substring(0, 4);

            return code;
        }

        public static string GetSign(string body, string key, string timeStamp)
        {
            if (body == null)
            {
                body = "";
            }
            byte[] bodyBuffer = Encoding.UTF8.GetBytes(body + timeStamp);
            byte[] tempBuffrt = new byte[200];
            if (bodyBuffer.Length > 200)
            {
                Array.Copy(bodyBuffer, 0, tempBuffrt, 0, 100);
                Array.Copy(bodyBuffer, bodyBuffer.Length - 100, tempBuffrt, 100, 100);
            }
            else
            {
                bodyBuffer.CopyTo(tempBuffrt, 0);
            }
            byte[] keyBuffer = Encoding.UTF8.GetBytes(key);
            int lenth = keyBuffer.Length;
            for (int i = 0; i < tempBuffrt.Length; i++)
            {
                tempBuffrt[i] = (byte)(tempBuffrt[i] ^ keyBuffer[i % lenth]);
            }
            return EncryptHelper.HashMD5Byte(tempBuffrt);
        }

        /// <summary>
        /// 检查电话号码是否正确
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool CheckPhoneIsAble(string input)
        {
            if (input.Length < 11)
            {
                return false;
            }
            string yidong = @"^(1\d{10})$";
            Regex regexYD = new Regex(yidong);
            if (regexYD.IsMatch(input))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  时间戳转本地时间-时间戳精确到毫秒
        /// </summary> 
        public static DateTime TimeStampToDateTime(long unix)
        {
            var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
            return dto.ToLocalTime().DateTime;
        }

        /// <summary>
        /// 获取当前时间戳
        /// </summary>
        /// <returns></returns> 
        public static long GetTimeStamp()
        {
            DateTimeOffset dto = new DateTimeOffset(DateTime.Now);
            return dto.ToUnixTimeMilliseconds();
        }

        /// <summary>
        /// 时间戳到秒
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStampSeconds()
        {
            DateTimeOffset dto = new DateTimeOffset(DateTime.Now);
            return dto.ToUnixTimeSeconds();
        }
        /// <summary>
        /// 获取指定时间的时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long GetTimeStamp(DateTime time)
        {
            DateTimeOffset dto = new DateTimeOffset(time);
            return dto.ToUnixTimeMilliseconds();
        }


        public static DateTime GetMonthFirstDay(DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, 1);
        }

        /// <summary>
        /// 获取当年某月第一天
        /// </summary>
        /// <param name="month"></param>
        /// <returns></returns>
        public static DateTime GetMonthFirstDay(int month, int year = -1)
        {
            if (year == -1)
            {
                year = DateTime.Now.Year;
            }
            if (month < 1 || month > 12)
            {
                throw new Exception("月份错误");
            }
            return new DateTime(year, month, 1);
        }

        /// <summary>
        /// 获取当年某月最后一天
        /// </summary>
        /// <param name="month"></param>
        /// <returns></returns>
        public static DateTime GetMonthLastDay(int month, int year = -1)
        {
            if (year == -1)
            {
                year = DateTime.Now.Year;
            }
            if (month < 1 || month > 12)
            {
                throw new Exception("月份错误");
            }
            DateTime dt = DateTime.Today;
            int monthDay = DateTime.DaysInMonth(year, month);
            return new DateTime(year, month, monthDay);
        }


        public static string GetBillNo(int userNo)
        {
            if (Volatile.Read(ref billIndex) > 99999)
            {
                billIndex = 0;
            }
            return $"{DateTime.Now:yyyyMMddHHmmss}{userNo}{GetTimeStampSeconds()}{Interlocked.Increment(ref billIndex).ToString("d5")}";
        }
    }
}
